{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 解法：双指针\n",
    "class Solution:\n",
    "    def lengthOfLongestSubstring(self, s: str) -> int:\n",
    "        i = 0\n",
    "        pre_i = 0\n",
    "        d = {}\n",
    "        max_len = 0\n",
    "        while i < len(s):\n",
    "            if s[i] in d and d[s[i]] >= pre_i:\n",
    "                pre_i = d[s[i]] + 1\n",
    "            if max_len < i - pre_i + 1:\n",
    "                max_len = i - pre_i + 1\n",
    "            d[s[i]] = i\n",
    "            i += 1\n",
    "        return max_len\n",
    "    \n",
    "    def lengthOfLongestSubstring(self, s: str) -> int:\n",
    "        if not len(s):\n",
    "            return 0\n",
    "        max_len = 1\n",
    "        pre_len = 1\n",
    "        for i in range(1, len(s)):\n",
    "            j = 1\n",
    "            while j <= pre_len:\n",
    "                if s[i - j] == s[i]:\n",
    "                    break\n",
    "                j += 1\n",
    "            pre_len = j\n",
    "            if max_len < j:\n",
    "                max_len = j\n",
    "        return max_len\n",
    "solution = Solution()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "solution.lengthOfLongestSubstring('pwekew') == 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "solution.lengthOfLongestSubstring('pwwkew') == 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "solution.lengthOfLongestSubstring('') == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
